#ifndef CONNECTION_H
#define CONNECTION_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>

static bool createConnection()
{
    QSqlDatabase db;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db = QSqlDatabase::database("qt_sql_default_connection");
    else
        db = QSqlDatabase::addDatabase("QSQLITE");
//    db.setHostName("127.0.0.1");
//    db.setUserName("root");
//    db.setPassword("");
    db.setDatabaseName("../info.db");
//    db.open();
    if(!db.open())
    {
        QMessageBox::critical(0,"错误","无法连接",QMessageBox::Yes);
    }
    QSqlQuery query(db);
    QString sql=QString("create table if not exists download("
                        "downloadFile varchar(255) not null primary key, "
                        "downloadFileName varchar(255) not null,"
                        "downloadFileDir varchar(255) not null,"
                        "url varchar(255) not null,"
                        "curTime varchar(63) not null,"
                        "totalBytes DOUBLE not null,"
                        "currentState varchar(20) not null,"
                        "account varchar(31) not null"
                        ")");
    if(query.exec(sql))
        qDebug()<<"download表建立成功！！";
    else
        qDebug()<<"表建立失败！！！！！";

    QSqlQuery query2(db);

    query2.prepare("insert into download"
                   "values('DICTIONARY','none','NoDownloadDir','none','none',0,'none','none');");


    if(query2.exec())
        qDebug()<<"dir插入成功！！";
    else
        qDebug()<<"表建立失败！！！！！";

    return true;
}
#endif // CONNECTION_H
